package cn.jhc.db;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.h2.jdbcx.JdbcDataSource;

import uuntil.Constants;

@WebListener
public class DbInitListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
		ServletContext context = event.getServletContext();
		String realPath = context.getRealPath("/store.db");
		JdbcDataSource dataSource = new JdbcDataSource();
		dataSource.setUrl("jdbc:h2:" + realPath);
		dataSource.setUser("sa");
		dataSource.setPassword("");
		context.setAttribute(Constants.H2_DATASOURCE, dataSource);
		
		MyDbUtils.createTables(dataSource);
		
	}


}